//
//  DRInviteFriendsView.m
//  DoctorRen
//
//  Created by 范伟健 on 2018/12/17.
//

#import "DRInviteFriendsView.h"
#import "DRInviteUserCell.h"
#import <Masonry/Masonry.h>
#import "DRFoodPairingModel.h"

@interface DRInviteFriendsView()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView *collectionView;

@property (nonatomic, strong) UILabel *titleLabel;

@property (nonatomic, strong) NSArray <DRFriendModel *>*dataArray;

@end

@implementation DRInviteFriendsView

-(instancetype)init{
    if (self = [super init]) {
        self.backgroundColor = [UIColor whiteColor];
        self.dataArray = @[];
        [self setupSubViews];
    }
    return self;
}

-(void)setDatas:(NSArray<DRFriendModel *> *)datas{
    self.dataArray = datas;
    [self.collectionView reloadData];
}

-(void) setupSubViews{
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(18);
        make.left.mas_equalTo(16);
        make.right.mas_equalTo(-16);
    }];
    
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.titleLabel.mas_bottom).mas_offset(16);
        make.left.mas_equalTo(16);
        make.right.mas_equalTo(self.inviteButton.mas_left).mas_offset(-16);
        make.bottom.mas_equalTo(-10);
        make.height.mas_equalTo(36);
    }];
    
    [self.inviteButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.collectionView);
        make.right.mas_equalTo(-16);
        make.width.mas_equalTo(108);
        make.height.mas_equalTo(32);
    }];
}

-(UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.textColor = UIColorFromHex(0x888888);
        _titleLabel.font = [UIFont systemFontOfSize:13];
        _titleLabel.numberOfLines = 0;
        _titleLabel.text = @"本方案仅限适宜无特殊情况人群，特殊情况人群以及过敏人群建议使用个人配餐";
        [self addSubview:_titleLabel];
    }
    return _titleLabel;
}

-(UIButton *)inviteButton{
    if (!_inviteButton) {
        _inviteButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [self addSubview:_inviteButton];
        _inviteButton.backgroundColor = There_Green;
        _inviteButton.titleLabel.font = [UIFont systemFontOfSize:15];
        [_inviteButton setTitle:@"邀请加入" forState:(UIControlStateNormal)];
        [_inviteButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _inviteButton.layer.cornerRadius = 16;
    }
    return _inviteButton;
}

-(UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.itemSize = CGSizeMake(36, 36);
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
        [self addSubview:_collectionView];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = [UIColor whiteColor];
        
        [_collectionView registerClass:[DRInviteUserCell class] forCellWithReuseIdentifier:@"DRInviteUserCell"];
    }
    return _collectionView;
}


-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    DRInviteUserCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DRInviteUserCell" forIndexPath:indexPath];
    [cell setUser:self.dataArray[indexPath.row]];
    cell.tapCloseButton = ^{
        NSLog(@"%ld",indexPath.row);
        if (self.delFriendBlock) {
            self.delFriendBlock(self.dataArray[indexPath.row]._id);
        }
    };
    return cell;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArray.count;
}


@end
